from distutils.log import info
from datetime import datetime
import scrapy
from second_demo.items import SecondDemoItem



class FirstSpider(scrapy.Spider):
    name = 'first'
    # allowed_domains = ['www.baidu.com']  只允许该级目录下的路由发起请求，一般关闭
    start_urls = ['http://www.baidu.com/']

    # 基于终端
    # def parse(self, response):
    #     data_list = []
    #     info_list = response.xpath('//*[@id="hotsearch-content-wrapper"]/li')
    #     for i in info_list:
    #         title = i.xpath('./a/span[2]//text()').extract()[0]
    #         print(title)
    #         data_list.append({str(datetime.now()): title})
    #     print(data_list)
    #     return data_list

    # 基于管道
    def parse(self, response):

        info_list = response.xpath('//*[@id="hotsearch-content-wrapper"]/li')
        for i in info_list:
            title = i.xpath('./a/span[2]//text()').extract()[0]
            now_time = str(datetime.now())
            
            # 将数据解析存储到item类型对象
            item = SecondDemoItem()
            item['title'] = title
            item['now_time'] = now_time

            yield item #将item交给管道

